package com.gce;
import com.feilong.lib.javassist.bytecode.ClassFile;
import com.feilong.lib.javassist.bytecode.FieldInfo;
import com.feilong.lib.javassist.bytecode.MethodInfo;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * feilong 工具包
 * @author wangzihao
 */
public class JdkClassLoader {


    public static void main(String[] args) throws Exception {
        File file = new File("/Users/chengen.gu/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.29/tomcat-embed-core-9.0.29.jar");

        try {
            URL url  = file.toURI().toURL();

            ClassLoader classLoader =new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());

            JarFile jarFile = new JarFile(file.getAbsolutePath());
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();
                String entryName = jarEntry.getName();

                if (entryName.endsWith(".class")) {

                    entryName = entryName.replace(".class", "").replaceAll("/", ".");

                    if ("org.apache.catalina.connector.CoyoteAdapter".equals(entryName)) {
                        InputStream in = classLoader.getResourceAsStream(jarEntry.getName());
                        DataInputStream dataInputStream = new DataInputStream(in);

                        ClassFile classFile1 = new ClassFile(dataInputStream);
                        List<FieldInfo>  fieldInfos = classFile1.getFields();
                        List<MethodInfo> methodInfos = classFile1.getMethods();
                        System.out.println(methodInfos);
                    }



                }
            }
            //ClazzInfoDefinition clazzInfoDefinition = ClassReader.read("");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
